package com.pys.beans;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang.StringUtils;

import com.pys.common.beans.BaseBean;
import com.pys.pojo.PysKullanici;
import com.pys.service.KullaniciService;
import com.pys.utils.FacesUtils;

@ViewScoped
@ManagedBean
public class SifreDegistirBean extends BaseBean implements Serializable {
	private final String CATEGORY_PASSWORD_POLICY = "PASSWORD_POLICY";
	private final String PROPERTY_MIN_KEYWORD_LENGTH = "MIN_KEYWORD_LENGTH";
	private final String PROPERTY_MAX_KEYWORD_LENGTH = "MAX_KEYWORD_LENGTH";

	private String keyword;
	private String newKeyword;
	private String newKeywordRetype;

	public SifreDegistirBean() {
	}

	public int getKeywordMinLength() {
		return 8;
		// ApplicationParameter.getInstance().getMandatoryParameterAsInt(
		// VpChannel.EPOS, CATEGORY_PASSWORD_POLICY,
		// PROPERTY_MIN_KEYWORD_LENGTH);
	}

	public int getKeywordMaxLength() {
		return 15;
		// ApplicationParameter.getInstance().getMandatoryParameterAsInt(
		// VpChannel.EPOS, CATEGORY_PASSWORD_POLICY,
		// PROPERTY_MAX_KEYWORD_LENGTH);
	}

	/*
	 * GETTER - SETTER
	 */
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getNewKeyword() {
		return newKeyword;
	}

	public void setNewKeyword(String newKeyword) {
		this.newKeyword = newKeyword;
	}

	public String getNewKeywordRetype() {
		return newKeywordRetype;
	}

	public void setNewKeywordRetype(String newKeywordRetype) {
		this.newKeywordRetype = newKeywordRetype;
	}

	@Override
	public void saveAction() {
		if (StringUtils.isBlank(keyword)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_MEVCUT_PAROLA_GEREKLI");
			return;
		}
		if (StringUtils.isBlank(newKeyword)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_YENI_PAROLA_GEREKLI");
			return;
		}
		if (StringUtils.isBlank(newKeywordRetype)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_TEKRAR_PAROLA_GEREKLI");
			return;
		}
		if (!newKeyword.equals(newKeywordRetype)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_PAROLALAR_FARKLI");
			return;
		}

		PysKullanici pysKullanici = FacesUtils.getUserSession()
				.getCurrentUser();

		if (pysKullanici.getSifre().equals(keyword)) {
			KullaniciService kullaniciService = new KullaniciService();
			pysKullanici.setSifre(newKeyword);
			kullaniciService.update(pysKullanici);
		} else {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_MEVCUT_PAROLA_HATALI");
			return;
		}
	}

	@Override
	public void refreshList() {
	}

	@Override
	public void deleteAction() {
	}

	@Override
	public void updateAction() {
	}

	@Override
	public void clear() {
	}

}
